package com.unitedvision.service.impl;

import java.util.List;

import javax.annotation.Resource;
import javax.persistence.EntityNotFoundException;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.unitedvision.persistence.domain.CustomerEntity;
import com.unitedvision.persistence.domain.CustomerTypeEntity;
import com.unitedvision.persistence.repository.CustomerRepository;
import com.unitedvision.persistence.repository.CustomerTypeRepository;
import com.unitedvision.service.CustomerService;

@Service
public class CustomerServiceImpl implements CustomerService {

	@Resource
	private CustomerRepository customerRepository;
	@Resource
	private CustomerTypeRepository customerTypeRepository;

	@Override
	@Transactional
	public void save(CustomerEntity customerEntity) {
		CustomerTypeEntity customerType = verifyCustomerType(customerEntity.getType());
		customerEntity.setType(customerType);
		
		customerRepository.save(customerEntity);
	}
	
	private CustomerTypeEntity verifyCustomerType(CustomerTypeEntity customerType) {
		if (customerType.getId() != 0)
			return customerType;
		
		CustomerTypeEntity fromDb = customerTypeRepository.findByName(customerType.getName());
		
		if (fromDb == null) {
			customerTypeRepository.save(customerType);
			fromDb = customerTypeRepository.findByName(customerType.getName());
		}
		
		return fromDb;
	}

	@Override
	@Transactional
	public void delete(CustomerEntity customerEntity) {
		customerRepository.delete(customerEntity);
	}

	@Override
	public CustomerEntity get(int id) {
		return customerRepository.findOne(id);
	}

	@Override
	public List<CustomerEntity> getAll() {
		return customerRepository.findAll();
	}

	@Override
	public List<CustomerEntity> getByNameLike(String name) {
		return customerRepository.findByNameContaining(name);
	}
	
	@Override
	public CustomerEntity getByName(String name) {
		return customerRepository.findByName(name);
	}

	@Override
	public List<CustomerEntity> getByType(CustomerTypeEntity type) {
		return customerRepository.findByType(type);
	}

	@Override
	public long count() {
		return customerRepository.count();
	}

	@Override
	public void save(CustomerTypeEntity customerType) {
		customerTypeRepository.save(customerType);
	}

	@Override
	public void delete(CustomerTypeEntity customerType) {
		customerTypeRepository.delete(customerType);
	}

	@Override
	public CustomerTypeEntity getType(int id) throws EntityNotFoundException {
		return customerTypeRepository.findOne(id);
	}

	@Override
	public List<CustomerTypeEntity> getAllType() throws EntityNotFoundException {
		return customerTypeRepository.findAll();
	}

	@Override
	public CustomerTypeEntity getTypeByName(String name) throws EntityNotFoundException {
		return customerTypeRepository.findByName(name);
	}

	@Override
	public long countType() {
		return customerTypeRepository.count();
	}
}
